// 1，导入相关函数
import { createRouter, createWebHashHistory, createWebHistory } from "vue-router";
// 导入一级页面文件
import Discover from '@/views/Discover.vue'
import Friend from '@/views/Friend.vue'
import Homepage from '@/views/Homepage.vue'
import PageNotFound from "@/views/PageNotFound.vue"
// 导入二级页面文件
import Recommend from "@/views/Recommend.vue";
import TopList from "@/views/TopList.vue";
import SongList from "@/views/SongList.vue";
// 创建登陆标志
import {ref} from "vue"
const isLogin = ref(false)
// 2，创建路由实例
const router = createRouter({
    // 指明路由器规则：当前创建是哈希模式，路径后面会带#
    // history: createWebHashHistory(),
    history: createWebHistory(),
    // 路由表规则
    routes: [
        // 将 / 重定向至 /discover 地址
        {
            path: '/',
            redirect: '/discover'
        },
        {
            path: '/discover',
            component: Discover,
            // 重定向
            redirect: '/discover/recommend',
            // 配置二级组件的路由映射规则
            children: [
                {
                    path: 'recommend',
                    component: Recommend
                },
                {
                    path: 'toplist',
                    component: TopList
                }, 
                {
                    path: 'songlist',
                    component: SongList
                }
            ]
        },
        {
            path: '/friend',
            component: Friend
        },
        {
            path: '/homepage',
            component: Homepage
        },
        // 配置404规则
        {
            path: '/:pathMatch(.*)*',
            component: PageNotFound
        }
    ]
})

// 添加路由守卫
router.beforeEach((to, from) => {
    // 我们设定规则：只有登录后的用户才能跳转至主页
    if(isLogin.value === false && to.path === "/homepage") {
        // 未登录但是想去主页
        alert("请先登录")
        return false
    }
    // 其它情况，一律放行
    return true
})
// 3,导出
export default router;






















